package _021_10_8

import "strings"

func wordPattern(pattern string, s string) bool {
	s1Map := make(map[byte]int)
	r1 := make([]int, len(pattern))
	r2 := make([]int, len(pattern))
	s2Map := make(map[string]int)
	k := 0
	for i := 0; i < len(pattern); i++ {
		i2, ok := s1Map[pattern[i]]
		if !ok {
			r1 = append(r1, k)
			k++
			continue
		}
		r1 = append(r1, i2)
	}
	sRet := strings.Fields(s)
	k = 0
	for i := 0; i < len(sRet); i++ {
		i2, ok := s2Map[sRet[i]]
		if !ok {
			r2 = append(r2, k)
			k++
			continue
		}
		r2 = append(r2, i2)
	}

	if len(r2) != len(r1) {
		return false
	}

	for i := 0; i < len(r1); i++ {
		if r1[i] != r2[i] {
			return false
		}
	}
	return true
}
